package com.kkb.merchant.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.merchant.center.api.BusinessInfoService;
import com.kkb.merchant.center.common.pojo.BusinessInfo;
import com.kkb.merchant.center.common.pojo.ShopInfo;
import com.kkb.merchant.center.common.pojo.UserInfo;
import com.kkb.merchant.center.server.mappers.BusinessInfoMapper;
import com.kkb.merchant.center.server.mappers.ShopInfoMapper;
import com.kkb.merchant.center.server.mappers.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class BusinessInfoServiceImpl implements BusinessInfoService {

    @Autowired
    private BusinessInfoMapper businessInfoMapper;

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public PageInfo<BusinessInfo> getBusinessList(Integer pageNo, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNo, pageSize);
        Example example = new Example(BusinessInfo.class);
        if(status>=0){
            example.createCriteria().andEqualTo("status",status);
        }
        List<BusinessInfo> businessInfos = businessInfoMapper.selectByExample(example);
        PageInfo<BusinessInfo> pageInfo = new PageInfo<>(businessInfos);
        return  pageInfo;
    }

    @Override
    public int selectCompNameExist(String compName) {
        Example example = new Example(BusinessInfo.class);
        example.createCriteria().andEqualTo("compName",compName);
        return businessInfoMapper.selectCountByExample(example);

    }

    @Override
    public int shopNameIsExist(String shopName) {
        Example example = new Example(ShopInfo.class);
        example.createCriteria().andEqualTo("shopName",shopName);
        return shopInfoMapper.selectCountByExample(example);
    }

    @Override
    public int shopUserNameIsExist(String shopUsername) {
        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("userName",shopUsername);
        return userInfoMapper.selectCountByExample(example);
    }

    /**
     * 商家注册
     * @param businessInfo 商家信息 包含店铺信息，用户信息
     * @return
     */
    @Override
    public int register(BusinessInfo businessInfo) {

        //添加商家信息
        businessInfo.setDeleteFlag((byte)0);
        businessInfo.setCreateTime(new Date());
        businessInfo.setUpdateTime(new Date());
        int  result = businessInfoMapper.insertUseGeneratedKeys(businessInfo);

        //获取最新的主键ID,也就是最新的商家ID
        Long businessId = businessInfo.getId();

        ShopInfo shopInfo = businessInfo.getShopInfo();

        //设置商家ID
        shopInfo.setBusinessId(businessId);

        shopInfo.setDeleteFlag((byte)0);
        shopInfo.setCreateTime(new Date());
        shopInfo.setUpdateTime(new Date());

        shopInfoMapper.insertUseGeneratedKeys(shopInfo);

        //获取当前的店铺ID
        Long shopId = shopInfo.getId();


        UserInfo userInfo = businessInfo.getUserInfo();

        userInfo.setShopId(shopId);


        //加密用户的密码
        userInfo.setUserPassword(new BCryptPasswordEncoder().encode(userInfo.getUserPassword()));

        userInfoMapper.insertSelective(userInfo);

        return result;
    }


}
